package com.hdcircles.autoform.widget;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;

/**
 * Created by hdcircles on 2016/10/17.
 */

public class DefaultItemTouchHelpCallback extends ItemTouchHelper.Callback {

    /**
     * Item 操作的回调
     */
    private OnItemTouchCallbackListener onItemTouchCallbackListener;

    /**
     * 是否可以拖拽
     */
    private boolean isCanDrag=true;

    /**
     * 是否可以被滑动
     */
    private boolean isCanSwipe=true;

    public DefaultItemTouchHelpCallback(OnItemTouchCallbackListener onItemTouchCallbackListener){
        this.onItemTouchCallbackListener=onItemTouchCallbackListener;
    }

    public void setOnItemTouchCallbackListener(OnItemTouchCallbackListener onItemTouchCallbackListener){
        this.onItemTouchCallbackListener=onItemTouchCallbackListener;
    }

    public void setCanDrag(boolean canDrag){
        isCanDrag=canDrag;
    }

    public void setCanSwipe(boolean canSwipe){
        isCanSwipe=canSwipe;
    }

    /**
     * 当item 被长按的时候是否可以被拖拽
     * @return
     */
    @Override
    public boolean isLongPressDragEnabled() {
        return isCanDrag;
    }

    /**
     * Item 是否可以被滑动(H:左右滑动，V:上下滑动)
     * @return
     */
    @Override
    public boolean isItemViewSwipeEnabled() {
        return isCanSwipe;
    }


    /**
     * 当用户拖拽或者滑动Item的时候需要告诉系统滑动或者拖拽的方向
     * @param recyclerView
     * @param viewHolder
     * @return
     */
    @Override
    public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        RecyclerView.LayoutManager layoutManager=recyclerView.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager){
            //flag 如果值是0，相当于这个功能被关闭
            int dragFlag=ItemTouchHelper.LEFT|ItemTouchHelper.RIGHT|ItemTouchHelper.UP|ItemTouchHelper.DOWN;
            int swipeFlag=0;
            //create make
            return makeMovementFlags(dragFlag,swipeFlag);
        }else if (layoutManager instanceof LinearLayoutManager){
            LinearLayoutManager linearLayoutManager= (LinearLayoutManager) layoutManager;
            int orientation=linearLayoutManager.getOrientation();

            int dragFlag=0;
            int swipeFlag=0;

            if (orientation==LinearLayoutManager.HORIZONTAL){//如果是横向的布局
                swipeFlag=ItemTouchHelper.UP|ItemTouchHelper.DOWN;
                dragFlag=ItemTouchHelper.LEFT|ItemTouchHelper.RIGHT;
            }else if (orientation==LinearLayoutManager.VERTICAL){//如果是竖向布局
                dragFlag=ItemTouchHelper.UP|ItemTouchHelper.DOWN;
                swipeFlag=ItemTouchHelper.RIGHT;
            }
            return makeMovementFlags(dragFlag,swipeFlag);
        }
        return 0;
    }


    /**
     * 当Item被拖拽时回调
     * @param recyclerView recyclerView
     * @param viewHolder 拖拽的ViewHolder
     * @param target 目的地的ViewHolder
     * @return
     */
    @Override
    public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
        if (null!=onItemTouchCallbackListener){
            onItemTouchCallbackListener.onMove(viewHolder.getAdapterPosition(),target.getAdapterPosition());
        }
        return false;
    }

    @Override
    public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
        if (null!=onItemTouchCallbackListener){
            onItemTouchCallbackListener.onSwiped(viewHolder.getAdapterPosition());
        }
    }

    public interface OnItemTouchCallbackListener{
        /**
         * 当某个Item被滑动删除的时候
         */
        void onSwiped(int adapterPosition);


        /**
         * 当两个Item位置互换的时候回调
         *
         * @param srcPosition 拖拽的item的position
         * @param targetPosition 目的地的Item的position
         * @return 处理了操作返回true,没有处理返回false
         */
        boolean onMove(int srcPosition,int targetPosition);
    }
}
